/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package org.amarena2d.examples;

import com.android1.amarena2d.nodes.Scene;
import org.amarena2d.examples.scenes.*;
import org.amarena2d.examples.scenes.admiral.AdmiralScene;
import org.amarena2d.examples.scenes.invader.InvaderLoadingScene;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Examples {

    public static final List<Class<? extends Scene>> examples = new ArrayList<Class<? extends Scene>>();

    static {
        examples.add(SpriteMoveExampleScene.class);
        examples.add(FontExampleScene.class);
        examples.add(ParticleSystemExampleScene.class);
        examples.add(ParticleTouchTestScene.class);
        examples.add(ButtonsExampleScene.class);
        examples.add(TiledTextureAtlasAnimationExampleScene.class);
        examples.add(ParallaxNodeExampleScene.class);
        examples.add(ScrolleableRepeatingBackgroundExampleScene.class);
        examples.add(RepeatingTextureExampleScene.class);
        examples.add(ScrollSpriteExampleScene.class);
        examples.add(DragSpriteExampleScene.class);
        examples.add(SpriteAnimationExampleScene.class);
        examples.add(InvaderLoadingScene.class);
        examples.add(LongPressExampleScene.class);
        examples.add(SequenceActionExampleScene.class);
        examples.add(AdmiralScene.class);
        examples.add(RepeatActionExampleScene.class);
        examples.add(AutoPackTextureAtlasExampleScene.class);
        examples.add(VirtualDPadExampleScene.class);
    }


    public static List<String> getNames() {
        List<String> names = new ArrayList<String>(examples.size());

        for (Class<? extends Scene> aClass : examples) {
            Example e = aClass.getAnnotation(Example.class);
            if (e != null) {
                names.add(e.name());
            }
        }
        return names;
    }

    public static Map<String, List<Item>> createExamples() {
        Map<String, List<Item>> map = new HashMap<String, List<Item>>();
        for (Class<? extends Scene> aClass : examples) {
            Example e = aClass.getAnnotation(Example.class);
            if (e != null) {
                Item i = new Item();
                i.name = e.name();
                i.category = e.category();
                i.sceneClass = aClass;

                List<Item> group = map.get(i.category);
                if (group == null) {
                    group = new ArrayList<Item>();
                    map.put(i.category, group);
                }
                group.add(i);
            }
        }
        return map;
    }


    public static Example findConfig(int index) {
        Class<? extends Scene> sceneClass = examples.get(index);
        try {
            return sceneClass.getAnnotation(Example.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Scene createScene(int index) {
        Class<? extends Scene> sceneClass = examples.get(index);
        try {
            return sceneClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static final class Item {
        public String name;
        public String category;
        public Class<? extends Scene> sceneClass;

    }
}
